package com.ruifu.proj.service.impl;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.ruifu.common.po.PmPMember;
import com.ruifu.common.po.PmPtMain;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.proj.dto.pmember.PmPMemberDTO;
import com.ruifu.proj.mapper.PmPMemberMapper;
import com.ruifu.proj.mapper.PmPtMainMapper;
import com.ruifu.proj.service.PmPMemberService;
import com.ruifu.proj.service.PmPtMainService;
import com.ruifu.proj.vo.pmember.PmPMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author nyr
 * @create 2019/9/18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PmPMemberServiceImpl implements PmPMemberService {
    @Autowired(required = false)
    private PmPMemberMapper pmPMemberMapper;

    @Autowired
    private PmPtMainService pmPtMainService;

    @Autowired(required = false)
    private PmPtMainMapper pmPtMainMapper;


    /**
     * 批量添加项目团队成员
     *
     * @param pmPMemberDTOList
     * @return
     */
    @Override
    public void insertPL(List<PmPMemberDTO> pmPMemberDTOList, CloudUser cloudUser) {
        //遍历团队成员集合
        for (PmPMemberDTO pmPMemberDTO : pmPMemberDTOList) {
            /*DTO转PO*/
            PmPMember pmPMember = BeanConverter.copyProperties(pmPMemberDTO, PmPMember.class);
            //存储到数据库
            pmPMemberMapper.insert(pmPMember, cloudUser);
        }
    }

    /**
     * 根据id删除成员配置
     *
     * @param pbId 团队成员记录表id
     * @return
     */
    @Override
    public void deleteById(Long pbId, CloudUser cloudUser) {
        //根据id查询当前团队成员记录
        PmPMember pmPMember = pmPMemberMapper.findById(pbId);
        //修改删除标记
        pmPMember.setPbDelFlag(true);
        //更新到数据库
        pmPMemberMapper.update(pmPMember, cloudUser);
    }

    /**
     * 根据项目id查询项目团队成员列表
     *
     * @param pbPtId 当前项目id
     * @return
     */
    @Override
    public List<PmPMemberVO> findByPbPtId(Long pbPtId, CloudUser cloudUser) {
        //根据id查询当前任务
        PmPtMain pmPtMain = pmPtMainMapper.findById(pbPtId);
        //声明对象，存储当前记录的所属项目
        PmPtMain projectPmPtMain;
        //判断当前任务的父任务id是否为空
        if (pmPtMain.getPtPId() != 0) {
            //根据任务的父任务id获取所属项目
            projectPmPtMain = pmPtMainService.recursionProject(pmPtMain.getPtPId());
        } else {
            //设置当前任务为根记录
            projectPmPtMain = pmPtMain;
        }
        //判断根记录是否没有属于任何项目
        if (projectPmPtMain == null){
            throw new RuntimeException("该任务不属于任何项目,无法获取项目团队人员列表");
        }
        //从所属项目的id查询项目团队配置
        List<PmPMember> pmPMemberList = pmPMemberMapper.findByPbPtId(projectPmPtMain.getPtId(), cloudUser);
        //判断是否未配置人员
        if (pmPMemberList == null || pmPMemberList.size() == 0) {
            throw new RuntimeException("该任务所属项目尚未配置团队人员,请联系相关人员进行添加");
        }
        /*po转vo*/
        return BeanConverter.copyList(pmPMemberList, PmPMemberVO.class);
    }
}
